﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Net.Mail;
using System.Net;

public partial class GetPassword : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    // 找回密码
    protected void Unnamed1_Click(object sender, EventArgs e)
    {
        try
        {
            if (this.txtusername.Text.Trim().ToString() != "")
            {
                BLL.Business.UserBLL userBLL = new BLL.Business.UserBLL();
                DataSet ds = new DataSet();
                ds = userBLL.SelectByName(this.txtusername.Text.Trim().ToString());
                if ((ds == null) ||
                    (ds.Tables.Count == 0) ||
                    (ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 0))
                { Response.Write("<script>alert('该用户不存在！')</script>"); }
                else
                {
                    String email = this.email.Text.Trim().ToString();
                    String problem = "";
                    String answer = "";
                    if (this.secproblem.Value != null && this.secproblem.SelectedIndex > 0 && this.answer.Text.Trim() != null)
                    {
                        problem = this.secproblem.Value;
                        answer = this.answer.Text.Trim().ToString();
                    }
                    if (ds.Tables[0].Rows[0]["User_Mailstr"].ToString() != email)
                    {
                        Response.Write("<script>alert('邮箱不对！')</script>");
                    }
                    else
                    {
                        if (ds.Tables[0].Rows[0]["User_Questionstr"].ToString() != "") // 没有选择安全问题
                        {
                            if (problem == "" || ds.Tables[0].Rows[0]["User_Questionstr"].ToString() != problem)
                            {
                                Response.Write("<script>alert('请选择你的安全问题！')</script>");
                            }
                            else if (answer != ds.Tables[0].Rows[0]["User_Answerstr"].ToString())
                            {
                                Response.Write("<script>alert('安全问题答案不对！')</script>");
                            }
                            else
                            {
                                if (SendMail(email, ds.Tables[0].Rows[0]["User_Passwordstr"].ToString()))
                                {
                                    this.panel1.Visible = true;
                                    this.labusername.Text = this.txtusername.Text.Trim().ToString();
                                }
                            }
                        }
                        else
                        {
                            if (SendMail(email, ds.Tables[0].Rows[0]["User_Passwordstr"].ToString()))
                            {
                                this.panel1.Visible = true;
                                this.labusername.Text = this.txtusername.Text.Trim().ToString();
                            }
                        }
                    }
                }
            }
            else
            {
                Response.Write("<script>alert('请输入用户名！')</script>");
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message.ToString() + "')</script>");
        }
    }

    protected bool SendMail(String destination, String password)
    {
        try
        {
            // 利用126SMTP发送
            MailAddress from = new MailAddress("changchun0528@126.com");// 发件地址
            MailAddress to = new MailAddress(destination);              // 目的地址
            MailMessage message = new MailMessage(from, to);
            message.Subject = "密码，系统发送，请勿回复！";// 设置邮件主题
            message.IsBodyHtml = true;                     // 设置邮件正文为html格式
            message.Body = "你的密码： " + password;         // 设置邮件内容
            SmtpClient client = new SmtpClient("smtp.126.com");
            //设置发送邮件身份验证方式
            //注意如果发件人地址是abc@def.com，则用户名是abc而不是abc@def.com
            client.Credentials = new NetworkCredential("changchun0528", "04764845154");
            client.Send(message);
            return true;

            ///*第二种，利用本地的smtp来发送邮件，失败*/
            //SmtpClient smtp =
            //new SmtpClient("192.168.1.105", 2525);
            //MailMessage message =
            //new MailMessage("changchun0528@126.com", "814636284@qq.com", "标题：测试一下iis发邮件", "内容：老翁，你好!哈哈");
            //smtp.Send(message);
            //return true;
        }
        catch
        {
            return false;
        }
    }
}